﻿''' <summary>
''' 记录表示行信息的管理器
''' </summary>
''' <remarks></remarks>
Public Class DisplayLineManager
    ''' <summary>
    ''' 表示行列表[存放内存行行号]
    ''' </summary>
    ''' <remarks></remarks>
    Dim LineNumberList As New List(Of Integer)
    ''' <summary>
    ''' 根据表示行号获得内存行号
    ''' </summary>
    ''' <param name="LineNumber">下标为1的行号</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetMemoryNoByDisplayNo(ByVal LineNumber As Integer) As Integer
        Return LineNumberList(LineNumber - 1)
    End Function
    ''' <summary>
    ''' 根据内存行号获得表示行号
    ''' </summary>
    ''' <param name="LineNumber">下标为1的行号</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetDisplayNoByMemoryNo(ByVal LineNumber As Integer) As Integer
        For i As Integer = 0 To LineNumberList.Count - 1
            If LineNumberList(i) = LineNumber Then Return i + 1
        Next
    End Function
    ''' <summary>
    ''' 增加一个表示行
    ''' </summary>
    ''' <param name="NewLineNumber"></param>
    ''' <remarks></remarks>
    Sub Add(ByVal NewLineNumber As Integer)
        LineNumberList.Add(NewLineNumber)
    End Sub
    ''' <summary>
    ''' 在制定位置后插入记录
    ''' </summary>
    ''' <param name="NewLineNumber"></param>
    ''' <remarks></remarks>
    Sub Insert(ByVal NewLineNumber As Integer, ByVal MemoryNumber As Integer)
        'TODO: 有的地方不能用ADD方法，请检查一下
        LineNumberList.Insert(NewLineNumber - 1, MemoryNumber)
        '修正余下行的内容
        For i As Integer = NewLineNumber To LineNumberList.Count - 1
            LineNumberList(i) += 1
        Next
    End Sub
    ''' <summary>
    ''' 所有表示行行数
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function Count() As Integer
        Return LineNumberList.Count
    End Function
    ''' <summary>
    ''' 清空行信息列表
    ''' </summary>
    ''' <remarks></remarks>
    Sub Clear()
        LineNumberList.Clear()
    End Sub
    ''' <summary>
    ''' 调整模式
    ''' </summary>
    ''' <remarks></remarks>
    Enum JustfiyMode As Byte
        ProcessFirstLine = 1
        ProcessRemainLine = 2
        ALL = 3
    End Enum
    ''' <summary>
    ''' 向表示列表中插入表示行[自动修改余下行的行号]
    ''' </summary>
    ''' <param name="startDisplayNumber">开始表示行号</param>
    ''' <param name="MemoryStartNumber">开始内存行号</param>
    ''' <param name="LineCount">插入行数</param>
    ''' <remarks></remarks>
    Sub InsertLinesByAddLine(ByVal startDisplayNumber As Integer, ByVal MemoryStartNumber As Integer, ByVal LineCount As Integer, ByVal Mode As JustfiyMode)
        If (Mode = JustfiyMode.ALL) Or (Mode = JustfiyMode.ProcessFirstLine) Then
            For i As Integer = 0 To LineCount - 1
                LineNumberList.Insert(startDisplayNumber - 1 + i, MemoryStartNumber + i)
            Next
        End If
        If (startDisplayNumber + LineCount = LineNumberList.Count + 1) And Mode = JustfiyMode.ALL Then Exit Sub
        '修正余下行的内容
        If (Mode = JustfiyMode.ALL) Or (Mode = JustfiyMode.ProcessRemainLine) Then
            For i As Integer = startDisplayNumber + LineCount - 1 To LineNumberList.Count - 1
                LineNumberList(i) += LineCount
            Next
        End If
    End Sub
    ''' <summary>
    ''' 从表示列表中删除表示行[自动修改余下行的行号]
    ''' </summary>
    ''' <param name="startDisplayNumber">开始表示行号</param>
    ''' <param name="LineCount">删除行数</param>
    ''' <remarks></remarks>
    Sub RemoveLinesByDelLine(ByVal startDisplayNumber As Integer, ByVal LineCount As Integer, ByVal Mode As JustfiyMode)
        '删除部分行
        If (Mode = JustfiyMode.ALL) Or (Mode = JustfiyMode.ProcessFirstLine) Then
            For i As Integer = 1 To LineCount
                LineNumberList.RemoveAt(startDisplayNumber - 1)
            Next
        End If
        If (startDisplayNumber > LineNumberList.Count) And Mode = JustfiyMode.ALL Then Exit Sub
        If (Mode = JustfiyMode.ALL) Or (Mode = JustfiyMode.ProcessRemainLine) Then
            '修正余下行的内容
            For i As Integer = startDisplayNumber - 1 To LineNumberList.Count - 1
                LineNumberList(i) -= LineCount
            Next
        End If
    End Sub
End Class


